package com.lbd.bankcard.service.impl;

import com.lbd.bankcard.bean.BankCard;
import com.lbd.bankcard.dao.BankCardRepository;
import com.lbd.bankcard.service.BankCardService;
import com.lbd.base.service.BaseServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by zzl on 2016/10/19.
 */
@Service
@Transactional
public class BankCardServiceImpl extends BaseServiceImpl<BankCard, BankCardRepository> implements BankCardService {
    @Autowired
    BankCardRepository bankcardRepository;

    @Override
    public Specification<BankCard> getSpecification(BankCard bankcard) {
        return null;
    }

    /**
     * 构建查询语句和参数
     *
     * @param bankcard
     * @param parameter
     * @return
     */
    @Override
    public String buildJpql(BankCard bankcard, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  BankCard t   ");
        if (bankcard != null) {
            if (StringUtils.isNotBlank(bankcard.getBankName())) {
                jpqlBuilder.append(" and  t.bankName like :bankName");
                parameter.put("bankName", "%" + bankcard.getBankName() + "%");
            }
            if (StringUtils.isNotBlank(bankcard.getShortName())) {
                jpqlBuilder.append(" and  t.shortName like :shortName");
                parameter.put("shortName", "%" + bankcard.getShortName() + "%");
            }
            if (StringUtils.isNotBlank(bankcard.getRechargeType())) {
                jpqlBuilder.append(" and  t.rechargeType = :rechargeType");
                parameter.put("rechargeType", bankcard.getRechargeType());
            }
        }
        jpqlBuilder.append(" order by orderValue asc ");
        return jpqlBuilder.toString();
    }


}
